const state = {
  items: [
    {
      name: '糖果',
      price: 15,
      count: 20,
    },
    {
      name: '薯条',
      price: 10,
      count: 30,
    },
    {
      name: '咖啡',
      price: 15,
      count: 40,
    },
  ],
  shopping: [
    {
      shoppingNumber: 1,
    },
    {
      shoppingNumber: 2,
    },
    {
      shoppingNumber: 3,
    },
  ],
};
const getters = {
  items: state => state.items,
  shopping: state => state.shopping,
  total: state => {
    return state.items
      .map((item, index) => item.price * state.shopping[index].shoppingNumber)
      .reduce((a, b) => a + b, 0);
  },
};
const mutations = {
  DECLINE_TODO: (state, index) => {
    if (state.shopping[index].shoppingNumber > 0) {
      state.shopping[index].shoppingNumber -= 1;
    } else {
      state.shopping[index].shoppingNumber;
      state.items.splice(index, 1);
      state.item = state.items;
    }
  },
  INCREASE_TODO: (state, index) => {
    if (state.shopping[index].shoppingNumber < state.items[index].count) {
      state.shopping[index].shoppingNumber += 1;
    } else {
      state.shopping[index].shoppingNumber;
    }
  },
};

const actions = {
  increase: ({ commit }, { index }) => {
    commit('INCREASE_TODO', index);
  },
  decline: ({ commit }, { index }) => {
    commit('DECLINE_TODO', index);
  },
};

export default {
  namespaced: true,
  state,
  getters,

  mutations,
  actions,
};
